package fr.upmc.cie;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Share extends ListActivity {
	private static final String[] types = new String[]{"SMS", "Email"};
	private String sujet="";
	private String content="";


	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);  
    	setListAdapter(new ArrayAdapter<String>(this, R.layout.share, types));
    	
    	Bundle bData = this.getIntent().getExtras();
    	sujet=bData.getString("title");
    	content=bData.getString("link");
    	
    	ListView lv = getListView();
    	lv.setTextFilterEnabled(true);

    	  lv.setOnItemClickListener(new OnItemClickListener() {
    		  @Override
  			public void onItemClick(AdapterView<?> parent,
  					android.view.View view, int position, long id) {
    			if(((TextView) view).getText()=="SMS"){
    				Uri uri = Uri.parse("smsto:");          
 	                Intent it = new Intent(Intent.ACTION_SENDTO, uri);          
 	                it.putExtra("sms_body", "Title : \""+sujet+"\"\n"+"Link : "+content);          
 	                startActivity(it); 
    			}else if(((TextView) view).getText()=="Email"){
    				// Setup the recipient in a String array  
	                 // String[] mailto = { "zongtian.wang@gmail.com" };  
	                  // Create a new Intent to send messages  
	                  Intent sendIntent = new Intent(Intent.ACTION_SEND);  
	                  // Write the body of theEmail  
	                  String emailBody =  "Title : \""+sujet+"\"\n"+"Link : "+content;  
	                  // Add attributes to the intent  
	                  sendIntent.setType("text/plain"); // use this line for testing  
                                                    // in the emulator  
                      //sendIntent.setType("message/rfc822"); // use this line for testing   
                                                        // on the real phone  
	                 // sendIntent.putExtra(Intent.EXTRA_EMAIL, mailto);  
	                  sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Share the article"+sujet);  
	                  sendIntent.putExtra(Intent.EXTRA_TEXT, emailBody);  
	                  startActivity(Intent.createChooser(sendIntent, "Email"));
    				
    			}
    		  }
  		  });
      
       
    }
}
